from conan import ConanFile
from conans.client.tools.version import Version


class CppEnvTestorConan(ConanFile):
    settings = "os", "arch", "compiler", "build_type"
    generators = "cmake", "cmake_find_package", "VirtualRunEnv"

    #模式
    #决定使用的库版本不相同
    #默认为mode1:能够完全编绎成功并成功运行的版本
    #3rd_bin_mode: Qt5.15.4 和 用其它三方自行编绎的模块
    options = {
        "shared": [True, False],
        "thirdparty_version": ["3rd_bin_mode",
                               "3rd_src_mode"],
    }

    default_options = {
        "shared": True,
        "thirdparty_version": "3rd_bin_mode",
    }
    
    @property
    def thirdparty_version(self):
        thirdparty_version = self.options.get_safe("thirdparty_version", "3rd_bin_mode")
        return thirdparty_version
    
    @property
    def compiler_version_major(self):
        # 获取编译器主版本号
        compiler_version=Version(self.settings.compiler.version)
        return compiler_version.major

    def requirements(self):
        if self.thirdparty_version == "3rd_bin_mode":
            if self.settings.os == "Windows":
                #为了尽快编绎采用以下适配三方库的版本进行兼容
                #=============核心依赖三方库=================
                #======相同版本=====
                self.requires("OpenSceneGraph/3.6.3@3rd/bin")
                self.requires("osgearth/2.10.1@3rd/bin")
                self.requires("sqlite3/3.32.3@3rd/bin")
                self.requires("tinyxml2/7.1@3rd/bin")
                self.requires("geos/3.5.1@3rd/bin")
                
                #======不同版本=====
                self.requires("gtest/1.8.1@")
                self.requires("gdal/3.0.4@3rd/bin")
                self.requires("libtiff/4.1.0@3rd/bin")
                self.requires("license/2.1@3rd/bin")
                self.requires("proj/7.0.0@3rd/bin")
                self.requires("qt/5.12.9@3rd/bin")
                self.requires("sdl/2.0.7@3rd/bin")
            elif self.settings.os == "Linux":
                # 处理不同的Qt二进制版本
                # ==========================
                qt = "qt/5.12.12@3rd/bin"
                if self.settings.compiler.version == "5":
                    qt = "qt/5.15.2@3rd/bin"
                self.requires(qt)
                # ==========================
                self.requires("OpenSceneGraph/3.6.3_2@3rd/bin")
                self.requires("osgearth/2.10.1_2@3rd/bin")
                self.requires("libpng/1.6.38@")
                
                #<直接依赖>三方库
                self.requires("sdl/2.0.7@3rd/bin")
                self.requires("gtest/1.8.1@")
                self.requires("sqlite3/3.38.5@")
                self.requires("proj/9.0.0@")
                
                #==================================
                #<间接依赖>三方库
                self.requires("gdal/3.4.3@")
                self.requires("geos/3.5.1@")
                self.requires("openssl/1.1.1q@")
                self.requires("tinyxml2/7.1.0@")
                
                #license依赖（简单License）
                self.requires("cryptopp/8.6.0@")

                self.requires("freetype/2.12.1@")
                self.requires("libtiff/4.3.0@")
        elif self.thirdparty_version == "3rd_src_mode":
            #self.requires("pybind11/2.13.2@")
            if self.settings.os == "Windows":
                # QT还未通过conan编绎出来，使用源码模式
                self.requires("qt/5.12.9@3rd/bin")
                self.requires("license/2.1@3rd/bin")
            elif self.settings.os == "Linux":
                # QT还未通过conan编绎出来，使用源码模式
                # 处理不同的Qt二进制版本
                # ==========================
                if self.settings.arch == "x86_64":
                    #qt = "qt/5.12.12@3rd/bin"
                    qt = "qt/5.15.7@"
                    if self.compiler_version_major == "5":
                        # gcc5下，conan模式没有编绎成功，使用二进制模式
                        qt = "qt/5.15.14@3rd/bin"
                        self.requires("expat/2.4.9@")
                elif self.settings.arch == "armv8":
                    qt = "qt/5.15.4@3rd/bin"
                else:
                    raise Exception("No Qt Version Support...")
                self.requires(qt)
                # ==========================
                self.requires("odbc/2.3.11@")
                #self.requires("license/2.1@3rd/bin")
                #license依赖（简单License）
                self.requires("cryptopp/8.6.0@")

            #==================================
            #公共版本三方库
            #==================================
            if self.settings.os == "Linux" and self.settings.compiler.version == "5":
                 self.requires("zlib/1.2.13@")
                 self.requires("bzip2/1.0.8@")
                 
            self.requires("OpenSceneGraph/3.6.300@")
            self.requires("osgearth/2.10@")
            self.requires("libpng/1.6.38@")

            #<直接依赖>三方库
            self.requires("sdl/2.26.0@")
            self.requires("gtest/1.8.1@")
            self.requires("sqlite3/3.38.5@")
            self.requires("proj/9.0.0@")

            #==================================
            #<间接依赖>三方库
            self.requires("libcurl/7.83.1@")
            self.requires("gdal/3.4.3@")
            self.requires("geos/3.10.2@")
            self.requires("openssl/1.1.1q@")
            self.requires("tinyxml2/7.1.0@")

            self.requires("freetype/2.12.1@")
            self.requires("libtiff/4.3.0@")
        else:
            raise SystemError("No Support ThirdParty Version....{}".format(self.thirdparty_version))

    def configure(self):
        # gcc5环境下会有错误，默认设置为动态库
        if self.settings.os == "Windows":
            # QT还未通过conan编绎出来，使用源码模式
            self.options['libcurl'].with_ldap=True
        elif self.settings.os == "Linux":
            # odbc的依赖
            self.options['odbc'].shared = True
            if self.compiler_version_major == "5":
                # 使用3rd 二制模式
                #self.options['zlib'].shared = True
                #self.options['bzip2'].shared = True
                #self.options["expat"].shared = True
                #self.options["freetype"].shared=True
                #self.options["freetype"].png_version="1.6.38"
                pass

            #self.options["qt"].shared=True
            #self.options["qt"].qtdeclarative=True
            #self.options["qt"].qtimageformats=True
            #self.options["qt"].qttranslations=True
            #self.options["qt"].qtquickcontrols=True
            #self.options["qt"].qtcharts=True
            #self.options["qt"].qtsvg=True

                
        # =============OpenSceneGraph=============
        self.options['OpenSceneGraph'].shared = True
        #此项属性需要为真：EWX的编绎有需此定义的函数处理
        self.options['OpenSceneGraph'].enable_deprecated_api = True
        #=========================================
        self.options['osgearth'].shared = True
        self.options['gdal'].shared = True
        self.options['gdal'].with_curl=True
        self.options['gdal'].tools=True
        self.options['sdl'].shared = True
        # self.options['qt'].shared = True
        
        if self.settings.os == "Windows":
            pass
        elif self.settings.os == "Linux":
            #主要是一些库是用的是二进制的打包方式，conan环境不够其动态实现编绎
            #二进制的版本库的属性设置
            pass


        #以下的配置需要动态库的版本，所以独立设置
        #self.options['gdal'].shared = True
        #self.options['geos'].shared = True
        #self.options['openssl'].shared = True
        #self.options['libpng'].shared = True
        
        #====================================================
        #以下是无用的代码，可以作为设置属性的参考代码
        #self.options['gdal'].with_png_version='png_v1254'

        #self.options['freetype'].shared = True
        #self.options['freetype'].with_png='png_v1254'

        #self.options['sdl'].shared = True
        #self.options['sqlite3'].shared = True
        #self.options['libpng'].shared = True